﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudFranceTriple.Models;

namespace CloudFranceTriple.WebRole.Areas.Portail.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class NewsController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        //
        // GET: /Portail/News/ShowNews
        [AllowAnonymous]
        [ChildActionOnly]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminNews")]
        public ActionResult ShowNews()
        {
            var _now = DateTime.UtcNow;
            var _yesterday = _now.AddDays(-1);
            return PartialView("_ShowNews", db.NewsSet.Where(news1 => news1.DateBegin <= _now).Where(news2 => (news2.DateEnd == null) || (news2.DateEnd >= _yesterday)).OrderBy(news3 => news3.Order).ToList());
        }

        //
        // GET: /Portail/News/

        [AllowAnonymous]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminNews")]
        public ActionResult Index()
        {
            return View(db.NewsSet.ToList());
        }

        //
        // GET: /Portail/News/Details/5

        [AllowAnonymous]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminNews")]
        public ActionResult Details(int id = 0)
        {
            NewsSet newsset = db.NewsSet.Find(id);
            if (newsset == null)
            {
                return HttpNotFound();
            }
            return View(newsset);
        }

        //
        // GET: /Portail/News/Create

        [Authorize(Roles = "RoleAdminNews")]
        public ActionResult Create()
        {
            //return View(new NewsSet { Title = null, Text = null, Order = 10, DateBegin = DateTime.UtcNow.Date, DateEnd = DateTime.UtcNow.AddDays(7).Date });
            return View();
        }

        //
        // POST: /Portail/News/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [Authorize(Roles = "RoleAdminNews")]
        public ActionResult Create(NewsSet newsset)
        {
            if (ModelState.IsValid)
            {
                newsset.Author = User.Identity.Name;
                newsset.AuthorIp = Request.UserHostAddress.Substring(0, Request.UserHostAddress.Length > 15 ? 15 : Request.UserHostAddress.Length);
                db.NewsSet.Add(newsset);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(newsset);
        }

        //
        // GET: /Portail/News/Edit/5

        [Authorize(Roles = "RoleAdminNews")]
        public ActionResult Edit(int id = 0)
        {
            NewsSet newsset = db.NewsSet.Find(id);
            if (newsset == null)
            {
                return HttpNotFound();
            }
            return View(newsset);
        }

        //
        // POST: /Portail/News/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [Authorize(Roles = "RoleAdminNews")]
        public ActionResult Edit(NewsSet newsset)
        {
            if (ModelState.IsValid)
            {
                newsset.Author = User.Identity.Name;
                newsset.AuthorIp = Request.UserHostAddress.Substring(0, Request.UserHostAddress.Length > 15 ? 15 : Request.UserHostAddress.Length);
                db.Entry(newsset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(newsset);
        }

        //
        // GET: /Portail/News/Delete/5

        [Authorize(Roles = "RoleAdminNews")]
        public ActionResult Delete(int id = 0)
        {
            NewsSet newsset = db.NewsSet.Find(id);
            if (newsset == null)
            {
                return HttpNotFound();
            }
            return View(newsset);
        }

        //
        // POST: /Portail/News/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminNews")]
        public ActionResult DeleteConfirmed(int id)
        {
            NewsSet newsset = db.NewsSet.Find(id);
            db.NewsSet.Remove(newsset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}